import 'package:flutter/material.dart';
import 'package:flutter_bsbdj/bean/comment_info.dart';
import 'package:flutter_bsbdj/utils/http_util.dart';
import 'package:dio/dio.dart';

class EpisodeComment extends StatefulWidget {
  int episoseId;

  EpisodeComment(this.episoseId) {
    assert(episoseId != null);
  }

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return CommentWidget();
  }
}

class CommentWidget extends State<EpisodeComment> {
  List<Comment> _data;
  int page = 1;
  ScrollController _scrollController = ScrollController();
  bool _hasLoadAll = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _scrollController.addListener(() {
      var px = _scrollController.position.pixels;
      var max = _scrollController.position.maxScrollExtent;
      if (px == max) {
        page++;
        _getComments();
      }
    });
    _getComments();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text("评论列表"),
      ),
      body: Container(
        child: ListView.builder(
          itemCount: _data == null ? 0 : _data.length + 1,
          itemBuilder: (content, index) {
            if (index == _data.length) {
              if (_hasLoadAll) {
                return Container(
                  height: 50,
                  alignment: Alignment.center,
                  child: Text(
                    "已加载全部评论",
                    style: TextStyle(fontSize: 10, color: Colors.grey),
                  ),
                );
              } else {
                return Container(
                  alignment: Alignment.center,
                  child: Container(
                    width: 30,
                    height: 30,
                    margin: EdgeInsets.all(5),
                    child: CircularProgressIndicator(),
                  ),
                );
              }
            } else {
              return _commentItem(_data[index]);
            }
          },
          controller: _scrollController,
        ),
      ),
    );
  }

  Widget _commentItem(Comment comment) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Container(
            padding: EdgeInsets.all(5),
            decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.all(Radius.circular(3))),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Row(
                  children: <Widget>[
                    Container(
                      width: 30,
                      height: 30,
                      margin: EdgeInsets.all(5),
                      child: ClipOval(
                        child: Image.network(comment.user.profile_image),
                      ),
                    ),
                    Expanded(
                      child: Text(comment.user.username),
                    ),
                    Text(
                      comment.ctime.replaceAll("T", " "),
                      style: TextStyle(color: Colors.grey, fontSize: 12),
                    )
                  ],
                ),
                Container(
                  margin: EdgeInsets.only(left: 30),
                  child: Text(comment.content),
                ),
              ],
            ),
          ),
          _commentChildren(comment.children),
          Container(
            color: Colors.black54,
            height: 1,
          ),
        ],
      ),
    );
  }

  Widget _commentChildren(List<Comment> comments) {
    if (comments == null || comments.length == 0) {
      return Container();
    } else {
      List<Widget> widgets = List<Widget>();
      for (var comment in comments) {
        widgets.add(Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Container(
              padding: EdgeInsets.all(5),
              decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(Radius.circular(3))),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Row(
                    children: <Widget>[
                      Container(
                        width: 30,
                        height: 30,
                        margin: EdgeInsets.all(5),
                        child: ClipOval(
                          child: Image.network(comment.user.profile_image),
                        ),
                      ),
                      Expanded(
                        child: Text(comment.user.username),
                      ),
                      Text(
                        comment.ctime.replaceAll("T", " "),
                        style: TextStyle(color: Colors.grey, fontSize: 12),
                      )
                    ],
                  ),
                  Container(
                    margin: EdgeInsets.only(left: 30),
                    child: Text(comment.content),
                  ),
                ],
              ),
            ),
          ],
        ));
      }
      return Container(
        margin: EdgeInsets.only(left: 30),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: widgets,
        ),
      );
    }
  }

  void _getComments() async {
    FormData formData = FormData();
    formData.add("id", widget.episoseId);
    formData.add("page", page);

    var rst =
        await HttpUtil().post("satinCommentApi", data: formData).then((result) {
      print("++++++++++++++++++++++");
      print(widget.episoseId);
      print(result);
      CommentInfo commentInfo = CommentInfo.fromJson(result.data);
      print(commentInfo);
      var d;
      if (page == 1) {
        d = commentInfo.data.hot.list;
      } else {
        d = _data;
      }
      d.addAll(commentInfo.data.normal.list);
      //只有当前widget还在 tree中才设置值
      if (mounted) {
        setState(() {
          _data = d;
          if (commentInfo.data.normal.info.count <= _data.length) {
            _hasLoadAll = true;
          } else {
            _hasLoadAll = false;
          }
        });
      }
    });
  }
}
